package cn.com.sp.demo.controller;

import java.io.File;
import java.io.IOException;
import java.net.Socket;
import java.util.Map;
import java.util.concurrent.LinkedBlockingQueue;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.context.request.async.DeferredResult;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.mvc.method.annotation.ResponseBodyEmitter;

import cn.com.sp.demo.SocketRunner;
import cn.com.sp.demo.bean.ThreadDemo;
import cn.com.sp.demo.bean.User;
import cn.com.sp.demo.service.IUserService;
import cn.com.sp.demo.socket.SocketManager;

@Controller
@RequestMapping(value = "test")
@EnableAutoConfiguration
public class TestController {

	@Autowired
	private IUserService userService;

	Integer a = 1;

	@Value("${test.string}")
	private String s;

	@ResponseBody
	@RequestMapping(value = "hello")
	public String hello() {
		return "Hello Spring Boot!";
	}

	@ResponseBody
	@RequestMapping(value = "helloStr")
	public String helloStr() {
		return "Hello Spring Boot!";
	}

	@RequestMapping(value = "redirect")
	public String redirect() {
		return "redirect:helloStr";
	}

	@RequestMapping(value = "helloModel")
	public Model helloModel(Model model) {
		model.addAttribute("message", "Hello Spring Boot!");
		return model;
	}

	@ResponseBody
	@RequestMapping(value = "getString")
	public String getString() {
		return s;
	}

	/**
	 * 通过url传参 /test/getUrlString/abc
	 *
	 * @author tcf
	 * @date 2018年10月31日 下午5:26:21
	 * @param urlString
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "getUrlString/{urlString}")
	public String getUrlString(@PathVariable String urlString) {
		// urlString="abc"
		return urlString;
	}

	/**
	 * 
	 *
	 * @author tcf
	 * @date 2018年10月31日 下午5:53:30
	 * @param request
	 * @param response
	 */
	@RequestMapping(value = "getStringResponse")
	public void getStringResponse(HttpServletRequest request, HttpServletResponse response) {
		String s = request.getParameter("test");
		try {
			response.getWriter().write("aaa" + s + "bbb");
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	/**
	 * 返回DeferredResult对象
	 *
	 * @return
	 */
	@ResponseBody
	@RequestMapping("testDeferredResult")
	public DeferredResult<String> testDeferredResult() {
		DeferredResult<String> deferredResult = new DeferredResult<>();
		ThreadDemo threadDemo = new ThreadDemo(deferredResult);
		threadDemo.start();
		return deferredResult;
	}

	/**
	 * 对DeferredResult的结果进行设置
	 * 
	 * @return
	 */
	@ResponseBody
	@RequestMapping("setDeferredResult")
	public String setDeferredResult() {

		return "succeed";
	}

	@ResponseBody
	@RequestMapping("getUserById")
	public User getUserById(int id) {
		return userService.getUserById(id);
	}

	@ResponseBody
	@RequestMapping("/events")
	public ResponseBodyEmitter handle() {
		ResponseBodyEmitter emitter = new ResponseBodyEmitter();
		// Save the emitter somewhere..
		for (int i = 0; i < 3; i++) {
			ThreadDemo threadDemo = new ThreadDemo(emitter);
			threadDemo.start();
		}
		return emitter;
	}

	/**
	 * 上传文件
	 *
	 * @author tcf
	 * @param name
	 * @param file
	 * @return
	 */
	@ResponseBody
	@RequestMapping(path = "/form")
	public String handleFormUpload(@RequestParam("name") String name, @RequestParam("file") MultipartFile file) {
		if (!file.isEmpty()) {
			try {
				byte[] bytes = file.getBytes();
				String path = "F:\\test3.xls";
				File file2 = new File(path);
				file.transferTo(file2);
				String s = new String(bytes);
			} catch (IOException e) {
				return "uploadFailure";
			}
			// store the bytes somewhere
			return "uploadSuccess";
		}
		return "uploadFailure2";
	}
	
	@ResponseBody
	@RequestMapping("/sendSocketMsg")
	public String sendSocketMsg(String socketName,String msg) {
		Map<String,SocketManager> socketMap  = SocketRunner.socketMap;
		if(socketMap.containsKey(socketName)){
			SocketManager socketManager = socketMap.get(socketName);
			socketManager.sendMsg(msg);
		}else{
			return "no socket";
		}
		return "success";
	}
}
